﻿Public Class frmStatComp
    Dim dsStatComp As DataSet
    Dim daStatComp As OleDb.OleDbDataAdapter
    Dim sql As String

    Private Sub frmStatComp_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        Me.Hide()
    End Sub

    Private Sub frmStatComp_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call gSubIconShow(Me)

        Call dbConnection()
        Call gSubMkCmbYear(cmbYear)
        Call gSubMkCmbCompany("", cmbCompany)
        cmbInOut.SelectedIndex = 0
        Call grid1Refresh("default")
    End Sub

    Private Sub btnGoStatistics_Click(sender As System.Object, e As System.EventArgs) Handles btnGoStatistics.Click
        Dim result As Integer = MessageBox.Show("거래처별 매입매출을 지금 조사 하시겠습니까?", "매입매출 조사", MessageBoxButtons.YesNo)
        If result = DialogResult.No Then
            Exit Sub
        ElseIf result = DialogResult.Yes Then
            btnSearch.Enabled = False
            btnSearchReset.Enabled = False
            Call gSubLookupOutAll()
            Call gSubLookupOutComp()
            Call gSubLookupInAll()
            Call gSubLookupInComp()
            MsgBox("매입매출 거래처별 현황 조사가 완료 되었습니다")
            btnSearch.Enabled = True
            btnSearchReset.Enabled = True
        End If
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Call grid1Refresh("")
    End Sub

    Private Sub btnNewWindow_Click(sender As System.Object, e As System.EventArgs) Handles btnNewWindow.Click

        Dim MDIFormStat As New frmStatComp
        MDIFormStat.ShowDialog()
    End Sub

    Private Sub btnSearchReset_Click(sender As System.Object, e As System.EventArgs) Handles btnSearchReset.Click

        Call gSubMkCmbYear(cmbYear)
        Call gSubMkCmbCompany("", cmbCompany)
        cmbInOut.SelectedIndex = 0
        Call grid1Refresh("")
    End Sub

    Private Sub cmbCompany_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbCompany.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbInOut_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbInOut.KeyPress
        e.Handled = True
    End Sub

    Private Sub cmbYear_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles cmbYear.KeyPress
        e.Handled = True
    End Sub

#Region "프로시져"

    Private Sub grid1Refresh(ByVal initFlag As String)
        Call dbConnection()
        Dim addSql As String = ""
        Dim valFindYear As String = ""
        Dim valFindCompany As String = ""
        Dim totalAmt As Double = 0
        Dim totalOrgAmt As Double = 0
        Dim totalRestAmt As Double = 0

        If cmbInOut.SelectedIndex = 0 Then
            addSql = addSql + " AND A.inOut='IN' "
        Else
            addSql = addSql + " AND A.inOut='OT' "
        End If
        If cmbYear.SelectedValue.ToString <> "-전체-" Then
            valFindYear = cmbYear.SelectedValue.ToString
        End If
        If valFindYear <> "" Then
            addSql = addSql + " AND A.Yr='" + valFindYear + "' "
        End If
        If cmbCompany.SelectedValue.ToString <> "0" Then
            valFindCompany = cmbCompany.SelectedValue.ToString
        End If
        If valFindCompany <> "" Then
            addSql = addSql + " AND A.typeCd=" + valFindCompany + " "
        End If

        If initFlag = "default" Then
            addSql = " AND A.flag='N'"
        End If

        dsStatComp = New DataSet

        sql = "SELECT A.Yr as 년도, IIF(A.typeCd=9999, 'TOTAL', B.company) as 거래처, A.m01 as 1월, A.m02 as 2월, A.m03 as 3월, A.m04 as 4월, A.m05 as 5월, A.m06 as 6월, A.m07 as 7월, A.m08 as 8월, A.m09 as 9월, A.m10 as 10월, A.m11 as 11월, A.m12 as 12월, A.seq, A.issueDt as 조사일 " & _
            "       FROM tblStatistics A LEFT JOIN tblCompany B ON A.typeCd=B.compCd " + _
            "       WHERE A.flag='Y' AND (A.types='all' OR A.types='comp') " + addSql + _
            "    ORDER BY A.Yr,IIF(A.typeCd=9999, '힁TOTAL', B.company) ASC "
        Try
            daStatComp = New OleDb.OleDbDataAdapter(sql, dbConn)
            daStatComp.Fill(dsStatComp, "StatList")

            grdStatComp.DataSource = dsStatComp.Tables("StatList")
            lbGrid1Cnt.Text = "총 " + dsStatComp.Tables("StatList").Rows.Count.ToString + " 건"

            dbConn.Close()
            Call setGrid1Design()

            grdStatComp.Columns(1).Frozen = True
        Catch err As ObjectDisposedException
            dbConn.Close()
            MsgBox(sql)

            MsgBox(err.Message, MsgBoxStyle.Critical, "오류")
        End Try
    End Sub

    Private Sub setGrid1Design()

        grdStatComp.Columns(0).Width = "60"
        grdStatComp.Columns(1).Width = "150"

        grdStatComp.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(2).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(2).Width = "90"
        grdStatComp.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(3).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(3).Width = "90"
        grdStatComp.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(4).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(4).Width = "90"
        grdStatComp.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(5).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(5).Width = "90"
        grdStatComp.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(6).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(6).Width = "90"
        grdStatComp.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(7).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(7).Width = "90"

        grdStatComp.Columns(8).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(8).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(8).Width = "90"
        grdStatComp.Columns(9).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(9).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(9).Width = "90"
        grdStatComp.Columns(10).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(10).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(10).Width = "90"
        grdStatComp.Columns(11).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(11).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(11).Width = "90"
        grdStatComp.Columns(12).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(12).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(12).Width = "90"
        grdStatComp.Columns(13).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        grdStatComp.Columns(13).DefaultCellStyle.Format = "n0"
        grdStatComp.Columns(13).Width = "90"
        grdStatComp.Columns(14).Visible = False
        grdStatComp.Columns(15).Width = "150"

        With grdStatComp.DefaultCellStyle
            .Alignment = DataGridViewContentAlignment.MiddleLeft

        End With

        grdStatComp.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        grdStatComp.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft

        For i = 0 To grdStatComp.Rows.Count - 1
            If IsDBNull(grdStatComp.Rows(i).Cells(1).Value) = False Then
                If grdStatComp.Rows(i).Cells(1).Value = "TOTAL" Then
                    grdStatComp.Rows(i).DefaultCellStyle.BackColor = Color.LavenderBlush
                    grdStatComp.Rows(i).DefaultCellStyle.Font = New Font(grdStatComp.DefaultCellStyle.Font, FontStyle.Bold)

                End If
            End If
        Next

    End Sub
#End Region

End Class